﻿using SBlogCore.Common.FluentValidatorExtend;
using SBlogCore.Common.Utilities.Extension;

namespace SBlogCore.WebAdminApplication.SysManager.Dto
{
    public class SysMenuEditInPut : SysMenuInPut
    {
        public long? Id { get; set; }
    }
    public class SysMenuInPut
    {
        /// <summary>
        /// 除了 layout 对应的 path 前面需要加 /，其余子路由都不要以/开头
        /// </summary>
        public string? Path { get; set; }
        /// <summary>
        /// 路由Name不能重复
        /// </summary>
        public string? Name { get; set; }
        /// <summary>
        /// 当设置 redirect 的时候该路由在面包屑导航中不可被点击
        /// </summary>
        public string? Redirect { get; set; }
        /// <summary>
        /// 组件
        /// </summary>
        public string? Component { get; set; }
        /// <summary>
        /// 排序
        /// </summary>
        public int Order { get; set; }

        /// <summary>
        /// 当设置 true 的时候该路由不会再侧边栏出现 如404，login等页面(默认 false)
        /// </summary>
        public bool? Hidden { get; set; } = false;
        /// <summary>
        /// 父级
        /// </summary>
        public long? ParentInfoId { get; set; }
        public SysMenuMetaInPut Meta { get; set; } = new SysMenuMetaInPut();
    }

    public class SysMenuMetaInPut
    {
        ///// <summary>
        ///// 当设置 true 的时候该路由不会再侧边栏出现 如404，login等页面(默认 false)
        ///// </summary>
        //public bool? Hidden { get; set; }
        /// <summary>
        /// 是否在左侧显示根目录
        /// </summary>
        public bool? AlwaysShow { get; set; }
        public bool? NoCache { get; set; }
        //public bool? Breadcrumb { get; set; }
        //public bool? Affix { get; set; }
        //public bool? NoTagsView { get; set; }
        //public string? ActiveMenu { get; set; }
        //public string? FollowAuth { get; set; }
        //public bool? CanTo { get; set; }
        public string? Title { get; set; }
        public string? Icon { get; set; }
    }

    /// <summary>
    /// 验证
    /// </summary>
    public class SysMenuInPutValidator : SblogCoreAbstractValidator<SysMenuInPut>
    {
        public SysMenuInPutValidator()
        {
            RuleFor(x => x.Path).NotNull().WithMessage("路径不能为空");
            RuleFor(x => x.Name).NotNull().WithMessage("名称不能为空");
            RuleFor(x => x.Component).NotNull().WithMessage("菜单不能为空");
            RuleFor(x => x.Path).Must(m => m != null && m.StartsWith("/"))
                .WithMessage("根级目录不能为空且要以“/”开头！")
                .When(w => !w.ParentInfoId.HasValue);

            RuleFor(x => x.Path).Must(m => m != null && !m.StartsWith("/"))
                .WithMessage("非根级目录不能为空且不应以“/”开头！")
                .When(w => w.ParentInfoId.HasValue);
        }
    }
    /// <summary>
    /// 验证
    /// </summary>
    public class SysMenuEditInPutValidator : SblogCoreAbstractValidator<SysMenuEditInPut>
    {
        public SysMenuEditInPutValidator()
        {
            RuleFor(x => x.Id).NotNull()
                .WithMessage("Id不能为空！");

            RuleFor(x => x.Path).Must(m => m != null && m.StartsWith("/"))
                .WithMessage("根级目录不能为空且要以“/”开头！")
                .When(w => !w.ParentInfoId.HasValue);

            RuleFor(x => x.Path).Must(m => m != null && !m.StartsWith("/"))
                .WithMessage("非根级目录不能为空且不应以“/”开头！")
                .When(w => w.ParentInfoId.HasValue);

            RuleFor(x => x.ParentInfoId).Must(m => !m.HasValue)
                .WithMessage("自己不能成为自己的父级！")
                .When(w => w.ParentInfoId==w.Id);
        }
    }
}
